package com.example.fastmybatis.service.impl;

import com.example.fastmybatis.entity.TUser;
import com.example.fastmybatis.dao.TUserMapper;
import com.example.fastmybatis.service.TUserService;
import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.gitee.fastmybatis.core.util.MapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class TUserServiceImpl implements TUserService {
    @Autowired
    TUserMapper tUserMapper;


    @Override
    public int addUser(TUser user) {
        tUserMapper.save(user);
        return 0;
    }

    @Override
    public int deleteUser(int id) {
        Query query = new Query();
        query.eq("id",id);
        return tUserMapper.deleteByQuery(query);
    }

    @Override
    public int updateUser(TUser user) {
        return tUserMapper.update(user);
    }

    @Override
    public int updateUser(Map<String, Object> user) {
        Query query = new Query();
        query.eq("id",Integer.valueOf(user.get("id").toString()));
        return tUserMapper.updateByMap(user,query);
    }

    @Override
    public TUser getUser(int id) {
        Query query = new Query();
        query.eq("id",id);
        TUser user = tUserMapper.getByQuery(query);
        return user;
    }

    @Override
    public int addUsers(List<TUser> users) {
        return tUserMapper.saveBatch(users);
    }

    @Override
    public Map<String, Object> getPageResult(int pageIndex,int pageSize) {

        Map<String,Object> map = new HashMap<>();
        Query query            = new Query();
        query.page(pageIndex,pageSize);

        List<TUser> users = tUserMapper.list(query);
        long total        = tUserMapper.getCount(query);

        map.put("list",users);
        map.put("total",total);
        return map;
    }

    @Override
    public PageInfo<TUser> getPageUsers(int pageIndex,int pageSize) {

        Query query            = new Query();
        query.page(pageIndex,pageSize);

        return MapperUtil.query(tUserMapper,query);
    }

    @Override
    public List<TUser> getUsersForOrder() {
        Query query = new Query();
        query.orderby("id", Sort.DESC);
        query.eq("isdel",0); //条件isdel==0
        return tUserMapper.list(query);
    }

}
